﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using DotNetNuke;
using DotNetNuke.Common.Utilities;
using DotNetNuke.Security;
using DotNetNuke.Services.Exceptions;
using DotNetNuke.Services.Localization;
using DotNetNuke.Services.FileSystem;
using DotNetNuke.Entities.Modules;
using DotNetNuke.Entities.Modules.Actions;
using DotNetNuke.UI.WebControls;

using CharityGamesOnline.Modules.Common;

namespace CharityGamesOnline.Modules.Account
{
    public partial class AccountLogView : CGOBase
    {
        private int accountUserID;
        public int AccountUserID
        {
            get
            {
                if (accountUserID == 0)
                {
                    accountUserID = UserId;                    
                }
                return accountUserID;
            }
            set
            {
                accountUserID = value;
            }
        }

        public bool ShowSectionHead
        {
            get
            {
                if (!Null.IsNull(Settings["ShowSectionHead"]))
                {
                    return Boolean.Parse(Settings["ShowSectionHead"].ToString());
                }
                else
                    return false;
            }
        }

        public bool ShowModuleName
        {
            get
            {
                if (!Null.IsNull(Settings["ShowModuleName"]))
                {
                    return Boolean.Parse(Settings["ShowModuleName"].ToString());
                }
                else
                    return false;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                if (!Page.IsPostBack)
                {
                    dshSectionHead.Visible = ShowSectionHead;

                    BindData(true);
                }
            }

            catch (Exception exc)
            {
                //Module failed to load 
                Exceptions.ProcessModuleLoadException(this, exc);
            }
        }

        protected void cgoPaging_PageChangeEvent(object sender, EventArgs e)
        {
            BindData(false);
        }

        private void BindData(bool refreshPaging)
        {
            AccountController ctrlAccount = new AccountController();
            List<AccountLogInfo> lstAccountLog;
            cgoPaging.Visible = true;
            grdAccountLog.Visible = true;
            lbNoResult.Visible = false;
            lstAccountLog = ctrlAccount.GetAccountLogs(PortalId, AccountUserID, refreshPaging, cgoPaging.PageSize, cgoPaging.CurrentPage);
            if (refreshPaging && lstAccountLog.Count > 0 && lstAccountLog[0].TotalRowCount > cgoPaging.PageSize)
            {                
                cgoPaging.TotalCount = lstAccountLog[0].TotalRowCount;
            }
            
            if(cgoPaging.TotalCount<cgoPaging.PageSize)
            {
                cgoPaging.Visible = false;
            }

            grdAccountLog.DataSource = lstAccountLog;
            grdAccountLog.DataBind();

            if (lstAccountLog.Count == 0)
            {
                grdAccountLog.Visible = false;
                lbNoResult.Visible = true;
            }
        }
    }
}